import OrgAPI from "@/api/org/org";
import { useTableData } from "@/components/SmTable/src/utils";

export const useTable = () => {
  /**
1-统筹单管理

2-查询
1-统筹单号-field1
2-车牌号-field2
3-车架号-field3

3-列表
1-统筹单号
2-打印单号
3-支付时间
4-统筹时间
5-统筹开始时间
6-统筹截至时间
7-被统筹人
8-车牌号
9-车辆种类
10-车辆初登日期
11-客户发票
12-统筹天数
13-剩余天数

*/

  const list: any = ref([]);
  const total = ref(0);
  const loading = ref(false);

  const { tableOptions, transformTableOptions } = useTableData();

  const fieldMap = {
    field1: "统筹单号",
    field2: "打印单号",
    field3: "支付时间",
    field4: "统筹时间",
    field5: "统筹开始时间",
    field6: "统筹截至时间",
    field7: "被统筹人",
    field8: "车牌号",
    field9: "车辆种类",
    field10: "车辆初登日期",
    field11: "客户发票",
    field12: "统筹天数",
    field13: "剩余天数",
    operate: {
      label: "操作",
      slot: "operate",
      fixed: "right",
      width: "180px",
    },
  };
  tableOptions.value = transformTableOptions(fieldMap);

  const queryParams: any = reactive({
    pageNum: 1,
    pageSize: 10,
  });

  /** 查询列表 */
  const getList = async () => {
    loading.value = true;
    try {
      const data = await OrgAPI.getList(queryParams);
      list.value = data.list;
      total.value = data.total;
    } finally {
      loading.value = false;
    }
  };

  function handleQuery() {
    queryParams.pageNum = 1;
    getList();
  }

  const queryFormRef = ref();
  /** 添加/修改操作 */
  const formRef = ref();
  const openForm = (type: string, id?: number) => {
    formRef.value.open(type, id);
  };

  /** 重置按钮操作 */
  const handleResetQuery = () => {
    queryFormRef.value.resetFields();
    nextTick(() => {
      handleQuery();
    });
  };

  return {
    tableOptions,
    list,
    total,
    loading,
    queryParams,
    getList,
    handleQuery,
    handleResetQuery,
    queryFormRef,
    formRef,
    openForm,
  };
};
